package com.shangcheng.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsFlashPromotionMapper;
import com.shangcheng.model.domain.SmsFlashPromotion;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 *
 */
@DubboService
public class SmsFlashPromotionApiImpl implements SmsFlashPromotionApi {

    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;

    /**
     * 添加活动
     *
     * @param flashPromotion
     * @return
     */
    @Override
    public boolean create(SmsFlashPromotion flashPromotion) {
        int insert = smsFlashPromotionMapper.insert(flashPromotion);
        return insert > 0;
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        int delete = smsFlashPromotionMapper.deleteById(id);
        return delete > 0;
    }

    /**
     * 获取活动详情
     *
     * @param id
     */
    @Override
    public SmsFlashPromotion findById(Integer id) {
        return smsFlashPromotionMapper.selectById(id);
    }

    /**
     * 根据id修改活动
     *
     * @param flashPromotion
     */
    @Override
    public SmsFlashPromotion update(SmsFlashPromotion flashPromotion) {
        smsFlashPromotionMapper.updateById(flashPromotion);
        return flashPromotion;
    }

    /**
     * 根据活动名称分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public IPage<SmsFlashPromotion> list(Integer pageNum, Integer pageSize, String keyword) {
        //1.设置page参数
        IPage page = new Page(pageNum, pageSize);
        //2.设置keyword参数查询条件
        LambdaQueryWrapper<SmsFlashPromotion> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(keyword), SmsFlashPromotion::getTitle, keyword)
                .orderByDesc(SmsFlashPromotion::getCreateTime)
                .orderByDesc(SmsFlashPromotion::getStatus);
        smsFlashPromotionMapper.selectPage(page, wrapper);
        return page;
    }
}
